package com.company.base.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.company.site.mode.common.DriveDate;

public class DateUtil {
	public static long SECOND_LONG = 1000l;

	private static long MINUTE_LONG = 60 * SECOND_LONG;

	private static long HOUR_LONG = 60 * MINUTE_LONG;

	private static long DAY_LONG = 24 * HOUR_LONG;
	
	private static long MONTH_LONG=30*DAY_LONG;
	
	private static long YEAR_LONG=365*DAY_LONG;

	private static String[] WEEKS = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };

	/**
	 * 判断两个date型日期是否是同一天
	 * @param date1
	 * @param date2
	 * @return
	 * true 代表是同一天
	 * false    不是同一天
	 */
	public static boolean isToday(Date date1,Date date2){
		if (date1 == null || date2 == null) {
			return false;
		}
		return date2.getTime() / DAY_LONG == date1.getTime() / DAY_LONG;
//		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//		String str1=sdf.format(date1);
//		String str2=sdf.format(date2);
//		return str1.equals(str2);
	}
	
	/**
	 * 计算两个日期之间相差的分钟数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Integer diffMinute(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.clear(Calendar.MINUTE);
		calendar.clear(Calendar.SECOND);
		calendar.clear(Calendar.MILLISECOND);
		return new Long((date2.getTime() - calendar.getTimeInMillis()) / MINUTE_LONG).intValue();
	}
	
	/**
	 * 计算两个日期之间相差的天数
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Integer diffDate(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return null;
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.clear(Calendar.MINUTE);
		calendar.clear(Calendar.SECOND);
		calendar.clear(Calendar.MILLISECOND);
		return new Long((date2.getTime() - calendar.getTimeInMillis()) / DAY_LONG).intValue();
	}

	/**
	 * 判断参数日期是否为今天
	 * @param date
	 * @return
	 */
	public static Boolean isToday(Date date) {
		return isToday(date, new Date());
	}

	/**
	 * 计算当期时间相差的日期
	 * @param field 日历字段.<br/>eg:Calendar.MONTH,Calendar.DAY_OF_MONTH,<br/>Calendar.HOUR_OF_DAY等.
	 * @param amount 相差的数值
	 * @return
	 */
	public static Date diffDate(int field, int amount) {
		Calendar c = Calendar.getInstance();
		c.add(field, amount);
		return c.getTime();
	}

	/**
	 * 字符串转换为Date类型
	 *
	 * @param dateValue
	 *            String 日期串
	 * @return Date
	 */
	public static String dateToString(Date dateValue, String strFormat) {
		SimpleDateFormat clsFormat = new SimpleDateFormat(strFormat);
		return clsFormat.format(dateValue);
	}

	/**
	 * 获取当天的统计时间,用于推荐<br/>
	 * 避免凌晨统计时没有当天数据,所以查询日期提前3个小时
	 * @return
	 */
	public static String getStatDate() {
		return dateToString(diffDate(Calendar.HOUR, -3), "yyyy-MM-dd");
	}

	public static Date getChartImgDate(int type) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR, -4);
		switch (type) {
		case 1:

			break;

		default:
			break;
		}

		return c.getTime();
	}

	/**
	 * 显示时间
	 * @param date 日期
	 * @param type 类型
	 * @return 返回需要显示的时间。当date为null或者type小于1或者type大于7时返回null。
	 */
	public static String showTime(Date date, int type) {
	
		if (CheckUtil.isEmpty(date) || type < 1 || type > 7) {
			return null;
		}
		Calendar now = Calendar.getInstance();
		Calendar pre = Calendar.getInstance();
		pre.setTime(date);
		long nowTime = now.getTimeInMillis();
		long preTime = pre.getTimeInMillis();

		if (type == 1) {
			if(nowTime<preTime){//如果是未来的时间显示 5分钟前
				pre.setTimeInMillis(new Date().getTime()-5*60000);
				return pushZero(pre.get(Calendar.HOUR_OF_DAY)) + ':' + pushZero(pre.get(Calendar.MINUTE));
			}
			String time = pushZero(pre.get(Calendar.HOUR_OF_DAY)) + ':' + pushZero(pre.get(Calendar.MINUTE));
			if (nowTime / DAY_LONG == preTime / DAY_LONG) {
				return time;
			}
			String daytime = pushZero(pre.get(Calendar.MONTH) + 1) + '-' + pushZero(pre.get(Calendar.DATE)) + ' ' + time;
			int year = pre.get(Calendar.YEAR);
			if (year == now.get(Calendar.YEAR)) {
				return daytime;
			}
			return String.valueOf(year) + '-' + daytime;
		} else if (type == 3) {
			return pushZero(pre.get(Calendar.MONTH) + 1) + '-' + pre.get(Calendar.DAY_OF_MONTH) + ' ' + pushZero(pre.get(Calendar.HOUR_OF_DAY)) + ':' + pushZero(pre.get(Calendar.MINUTE));
		} else if(type == 2){
			long timeDiff = nowTime - preTime;
			if(timeDiff<0){//如果是未来的时间显示 5分钟前
				return String.valueOf(5) + "分钟前";
			}
			long secondDiff = timeDiff / SECOND_LONG;
			if (secondDiff < 60) {
				return String.valueOf(secondDiff == 0 ? 1 : secondDiff) + "秒前";
			}
			long minuteDiff = timeDiff / MINUTE_LONG;
			if (minuteDiff < 60) {
				return String.valueOf(minuteDiff) + "分钟前";
			}
			long hourDiff = timeDiff / HOUR_LONG;
			long dayDiff = timeDiff / DAY_LONG;
			if (hourDiff < 24) {
				return String.valueOf(hourDiff) + "小时前";
			}
			if (dayDiff < 365) {
				if(dayDiff == 0){
					dayDiff = 1;
				}
				return String.valueOf(dayDiff) + "天前";
			}
			long monthDiff = (now.get(Calendar.MONTH) - pre.get(Calendar.MONTH) + 12) % 12;
			long yearDiff = now.get(Calendar.YEAR) - pre.get(Calendar.YEAR);
			if (yearDiff != 0) {
				return String.valueOf(yearDiff) + "年前";
			}
			return String.valueOf(monthDiff) + "月前";
		}else if(type == 4){
			StringBuilder dayTime = new StringBuilder();
			dayTime.append(pushZero(pre.get(Calendar.MONTH) + 1))
			.append('月').append(pushZero(pre.get(Calendar.DATE)))
			.append('日').append("(")
			.append(WEEKS[pre.get(Calendar.DAY_OF_WEEK) - 1])
			.append(")");
			return dayTime.toString();
		} else if (type==7){
		  /**
		   * 计算与当前时间的差
		   */
			
			long timeDiff = preTime-nowTime;
			if(timeDiff<0){//如果是过去的时间，返回5分钟
				return "5分钟";
			}
			StringBuffer sb=new StringBuffer();
			long yearDiff=timeDiff/YEAR_LONG;
			if(yearDiff>0){
				sb.append(yearDiff).append("年");
			}
			long monthDiff=(timeDiff-yearDiff*YEAR_LONG)/MONTH_LONG;
			if(monthDiff>0){
				sb.append(monthDiff).append("月");
			}
			long dayDiff=(timeDiff-yearDiff*YEAR_LONG-monthDiff*MONTH_LONG)/DAY_LONG;
			if(dayDiff>0){
				sb.append(dayDiff).append("天");
			}
			long hourDiff = (timeDiff-yearDiff*YEAR_LONG-monthDiff*MONTH_LONG-dayDiff*DAY_LONG) / HOUR_LONG;
			if(hourDiff>0){
				sb.append(hourDiff).append("小时");
			}
			long minuteDiff = (timeDiff-yearDiff*YEAR_LONG-monthDiff*MONTH_LONG-dayDiff*DAY_LONG-hourDiff*HOUR_LONG) / MINUTE_LONG;
			if(minuteDiff>0){
				sb.append(minuteDiff).append("分");
			}else{
				sb.append(1).append("分");
			}
			return sb.toString();
		}else if (type==6) {
			String str = "";
			int days = diffDate(date, now.getTime());
			if (days == 0) {
				str = "(今天)";
			} else if (days == 1) {
				str = "(昨天)";
			} else if (1 < days && days < 10) {
				str = "(" + days + "天前)";
			} else {
				str = "(有效期内)";
			}
			if (now.get(Calendar.YEAR) == pre.get(Calendar.YEAR)) {
				return new SimpleDateFormat("MM-dd").format(date) + str;
			} else {
				return new SimpleDateFormat("yyyy-MM-dd").format(date) + str;
			}
		}else{
			SimpleDateFormat fmat = new SimpleDateFormat("yyyy-MM-dd");
			return fmat.format(date);
		}
	}
	
	public static String pushZero(int i) {
		return (i < 10 ? "0" : "") + i;
	}


	/**
	 * 将字符串转换为时间
	 *
	 * @param strValue
	 *            String 字符串日期
	 * @return Date
	 */
	public static Date stringToDate(String strValue) {
		if (CheckUtil.isEmpty(strValue)) {
			return null;
		}
		SimpleDateFormat clsFormat = null;
		if (strValue.length() > 19)
			strValue = strValue.substring(0, 19);
		if (strValue.length() == 19)
			clsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		else if (strValue.length() == 10)
			clsFormat = new SimpleDateFormat("yyyy-MM-dd");
		else if (strValue.length() == 14)
			clsFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		else if (strValue.length() == 8)
			clsFormat = new SimpleDateFormat("yyyyMMdd");
		else if (strValue.length() == 16)
			clsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		ParsePosition pos = new ParsePosition(0);
		if (!CheckUtil.isEmpty(clsFormat)) {
			return clsFormat.parse(strValue, pos);
		}
		return null;
	}

	public static String formatDaguuDate(Date date,int type,String format){
		if (!CheckUtil.isEmpty(format)) {
			return dateToString(date, format);
		}
		Calendar now = Calendar.getInstance();
		Calendar pre = Calendar.getInstance();
		if (date == null) {
			throw new RuntimeException("Time格式化出错，日期为null");
		}
		pre.setTime(date);
		long nowTime = now.getTimeInMillis();
		long preTime = pre.getTimeInMillis();

		if (type == 1 || type == 3 || type ==4) {
			// if(nowTime<preTime){//如果是未来的时间显示 5分钟前
			// pre.setTimeInMillis(new Date().getTime()-5*60000);
			// String time =new
			// StringBuffer().append(pushZero(pre.get(Calendar.HOUR_OF_DAY))).append(':').append(pushZero(pre.get(Calendar.MINUTE))).toString();
			// return time;
			// }
			String time = new StringBuffer()
					.append(pushZero(pre.get(Calendar.HOUR_OF_DAY)))
					.append(':').append(pushZero(pre.get(Calendar.MINUTE)))
					.toString();
			if (nowTime / DAY_LONG == preTime / DAY_LONG && type != 4) {
				return time;
			}
			String daytime = "";
			String yearSplit = "-";
			if (type == 3) {
				daytime = new StringBuffer()
						.append(pushZero(pre.get(Calendar.MONTH) + 1))
						.append('-').append(pushZero(pre.get(Calendar.DATE)))
						.append("(")
						.append(WEEKS[pre.get(Calendar.DAY_OF_WEEK) - 1])
						.append(")").append(' ').append(time).toString();
			}else if (type == 4) {
//				daytime = new StringBuffer()
//				.append(pushZero(pre.get(Calendar.MONTH) + 1))
//				.append('-').append(pushZero(pre.get(Calendar.DATE)))
//				.append("(")
//				.append(WEEKS[pre.get(Calendar.DAY_OF_WEEK) - 1])
//				.append(")").append(' ').toString();
				daytime = new StringBuffer()
					.append(pushZero(pre.get(Calendar.MONTH) + 1))
					.append('月').append(pushZero(pre.get(Calendar.DATE)))
					.append('日').append("(")
					.append(WEEKS[pre.get(Calendar.DAY_OF_WEEK) - 1])
					.append(")").toString();
				yearSplit = "年";
			}  else {
				daytime = new StringBuffer()
						.append(pushZero(pre.get(Calendar.MONTH) + 1))
						.append('-').append(pushZero(pre.get(Calendar.DATE)))
						.append(' ').append(time).toString();
			}
			int year = pre.get(Calendar.YEAR);
			if (year == now.get(Calendar.YEAR)) {
				return daytime;
			}

			return new StringBuffer().append(year).append(yearSplit).append(daytime)
					.toString();
		} else if (type == 2) {
			long timeDiff = nowTime - preTime;
			if (timeDiff < 0) {// 如果是未来的时间显示 5分钟前
				return new StringBuffer().append(5).append("分钟前").toString();
			}
			long secondDiff = timeDiff / SECOND_LONG;
			if (secondDiff < 60) {
				return new StringBuffer()
						.append(secondDiff == 0 ? 1 : secondDiff).append("秒前")
						.toString();
			}
			long minuteDiff = timeDiff / MINUTE_LONG;
			if (minuteDiff < 60) {
				return new StringBuffer().append(minuteDiff).append("分钟前")
						.toString();
			}
			long hourDiff = timeDiff / HOUR_LONG;
			long dayDiff = timeDiff / DAY_LONG;
			if (hourDiff < 24) { // & (now.get(Calendar.DATE) ==
									// pre.get(Calendar.DATE) ||
									// (now.get(Calendar.HOUR_OF_DAY) < 6 &&
									// pre.get(Calendar.HOUR_OF_DAY) > 17))) {
				return new StringBuffer().append(hourDiff).append("小时前")
						.toString();
			}
			if (dayDiff < 365) {
				if (dayDiff == 0) {
					dayDiff = 1;
				}
				return new StringBuffer().append(dayDiff).append("天前")
						.toString();
			}
			// if (dayDiff / 7 < 56) {
			// return new StringBuffer().append(dayDiff /
			// 7).append("周前").toString();
			// }
			long monthDiff = (now.get(Calendar.MONTH) - pre.get(Calendar.MONTH) + 12) % 12;
			long yearDiff = now.get(Calendar.YEAR) - pre.get(Calendar.YEAR);
			if (yearDiff != 0) {
				return new StringBuffer().append(yearDiff).append("年前")
						.toString();
			}
			return new StringBuffer().append(monthDiff).append("月前").toString();
		}
		return null;
	}

	/**
	 * 判断当前时间是否是工作日(周一至周五)的9:30至18:00之间
	 */
	public static boolean isWorkDay(){
		boolean res = true;
		Calendar pre = Calendar.getInstance();
		pre.setTime(new Date());
		Integer week = pre.get(Calendar.DAY_OF_WEEK);
		StringBuffer buffer = new StringBuffer();
		if(pre.get(Calendar.MINUTE)<10){
			buffer.append(pre.get(Calendar.HOUR_OF_DAY)).append("0"+pre.get(Calendar.MINUTE));
		}else{
			buffer.append(pre.get(Calendar.HOUR_OF_DAY)).append(pre.get(Calendar.MINUTE));
		}
		if(week == 1 || week ==7 || Integer.parseInt(buffer.toString()) < 930  ||  Integer.parseInt(buffer.toString()) > 1800){
			res = false;
		}
	
		return res;
	}

	/**
	 * 计算给定时间XX天的时间
	 * @param diffDay 天数差
	 * @param isBigger 是比当前时间大还是小
	 * @author jeremy.Tan
	 * @return date
	 */
	public static Date getDay(Date date, Integer diffDay) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, diffDay);
		return cal.getTime();
	}
	
	
	/**
	 * 取得包括今天在内的未来七天的日期
	 * @return
	 */
	public static List<DriveDate> getWeekDays(){
		List<DriveDate> weeks=new ArrayList<DriveDate>();
		for(int i=0;i<7;i++){
			DriveDate ddate=new DriveDate();
			Date day=getDay(new Date(),i);
			ddate.setDay(dateToString(day,"yyyy-MM-dd"));
			ddate.setWeek(showTime(day,4));
			weeks.add(ddate);
			System.out.println("day:"+ddate.getDay()+" week:"+ddate.getWeek());
		}
		return weeks;
	}
	
	
}
